#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 1;
template<typename TYPE_X, typename TYPE_Z> class KernelSilu;

template<> class KernelSilu<half, half> {
public:
    __aicore__ inline KernelSilu() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ half*)x + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ half*)z + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(half));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileDataNum * sizeof(half));
        pipe.InitBuffer(tmpBuf0, this->tileDataNum * sizeof(float));
        pipe.InitBuffer(tmpBuf1, this->tileDataNum * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
      AscendC::LocalTensor<half> zLocal = outQueueZ.AllocTensor<half>();
      AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
      AscendC::LocalTensor<float> tmpTensor1 = tmpBuf1.Get<float>();
      
      float sign = -1.0;
      float scalar = 1.0;
      AscendC::Cast(tmpTensor1, xLocal, AscendC::RoundMode::CAST_NONE, this->processDataNum);
      AscendC::Muls(tmpTensor0, tmpTensor1, sign, this->processDataNum);
      AscendC::Exp(tmpTensor0, tmpTensor0, this->processDataNum);
      AscendC::Adds(tmpTensor0, tmpTensor0, scalar, this->processDataNum);
      AscendC::Div(tmpTensor1, tmpTensor1, tmpTensor0, this->processDataNum);
      AscendC::Cast(zLocal, tmpTensor1, AscendC::RoundMode::CAST_NONE, this->processDataNum);

      outQueueZ.EnQue<half>(zLocal);
      inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<half> zLocal = outQueueZ.DeQue<half>();  
      AscendC::DataCopy(zGm[progress * this->tileDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<half> xGm;
    AscendC::GlobalTensor<half> zGm;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0, tmpBuf1;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

template<typename TYPE_X, typename TYPE_Z> class KernelSilu {
public:
    __aicore__ inline KernelSilu() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ TYPE_Z*)z + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_Z));
        pipe.InitBuffer(tmpBuf0, this->tileDataNum * sizeof(TYPE_X));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.DeQue<TYPE_X>();
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.AllocTensor<TYPE_Z>();
      AscendC::LocalTensor<TYPE_X> tmpTensor0 = tmpBuf0.Get<TYPE_X>();

      // TYPE_X sign = -1.0;
      // TYPE_X scalar = 1.0;
      // AscendC::Muls(tmpTensor0, xLocal, sign, this->processDataNum);
      // AscendC::Exp(tmpTensor0, tmpTensor0, this->processDataNum);
      // AscendC::Adds(tmpTensor0, tmpTensor0, scalar, this->processDataNum);
      // AscendC::Div(zLocal, xLocal, tmpTensor0, this->processDataNum);

      // AscendC::Silu(zLocal,xLocal,this->processDataNum);

      TYPE_X scalar = 1.0;
      AscendC::Exp(tmpTensor0, xLocal, this->processDataNum);
      AscendC::Adds(tmpTensor0, tmpTensor0, scalar, this->processDataNum);
      AscendC::Div(tmpTensor0, xLocal, tmpTensor0, this->processDataNum);
      AscendC::Exp(xLocal, xLocal, this->processDataNum);
      AscendC::Mul(zLocal, xLocal, tmpTensor0, this->processDataNum);

      outQueueZ.EnQue<TYPE_Z>(zLocal);
      inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.DeQue<TYPE_Z>();  
      AscendC::DataCopy(zGm[progress * this->tileDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Z> zGm;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

extern "C" __global__ __aicore__ void silu_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    if constexpr (std::is_same_v<DTYPE_X, half>)
    {
        KernelSilu<half, half> op;
        op.Init(x, z, tiling_data.smallCoreDataNum, 
                tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
                tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
                tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
                tiling_data.tailBlockNum);  
        op.Process();
    }
    else if constexpr (std::is_same_v<DTYPE_X, float>)
    {
        KernelSilu<float, float> op;
        op.Init(x, z, tiling_data.smallCoreDataNum, 
                tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
                tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
                tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
                tiling_data.tailBlockNum);  
        op.Process();
    }
}
